<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .router-link-active {
      color: red;
    }
  </style>
</head>

<body>
  <div id="app">
    <!-- 视图定义 router-view组件 路由组件的出口 匹配路由组件就会在router-view位置处渲染 -->
    <router-link tag="button" to="/home">到首页</router-link>
    <router-link tag="button" to="/news">到新闻页</router-link>
    <router-link replace tag="button" to="/about">到关于我们页</router-link>
    <hr>
    <router-view></router-view>
  </div>
  <script src="./vue.js"></script>
  <script src="./vue-router.js"></script>

  <script>
    // 先准备  三个 路由组件  config
    const Home = {
      template: `
        <div>
          <h2>这是首页</h2>  
        </div>
      `
    }
    const News = {
      template: `
        <div>
          <h2>这是新闻页</h2>  
        </div>
      `
    }
    const About = {
      template: `
        <div>
          <h2>这是关于我们页</h2>  
        </div>
      `
    }
    const NotFound = {
      template: `
        <div>
          <h2>404啦</h2>  
        </div>
      `
    }
    const Login = {
      template: `
        <div>
          <h2>登录页</h2>  
          <button @click="doLogin">登录</button>
        </div>
      `,
      methods: {
        doLogin() {
          setTimeout(() => {
            const token = 'dwt4er5yu76itrs';
            // 立即缓存起来
            localStorage.setItem('token',token);
            // 调回首页
            if (this.$route.query.from) {
              this.$router.push(this.$route.query.from)
            }else{
              this.$router.push('/home');
            }
          },1000)
        }
      }
    }
    const routes = [
      {
        path: '/',
        redirect: '/home'
      },
      {
        path: '/home', // 路由path
        name: 'home',
        meta: {
          needLogin: false
        },
        component: Home
      },
      {
        path: '/news',
        name: 'news',
        meta: {
          needLogin: false
        },
        component: News
      },
      {
        path: '/about',
        meta: {
          needLogin: true
        },
        name: 'about',
        component: About
      },
      /* 
        vue提供了一个特殊的path * 可以匹配任意路由地址 优先级是最低的
      */
      {
        path: '/login',
        name: 'login',
        meta: {
          needLogin: false
        },
        component: Login
      },
      {
        path: '*',
        component: NotFound
      }
    ]
    // 定义路由实例对象并 注册 路由规则
    const router = new VueRouter({
      // routes 路由规则
      routes
    });
    const isLogin = () => !!localStorage.getItem('token');
    // 定义全局前置导航守卫
    router.beforeEach((to,from,next) => {
      if(to.meta.needLogin) {
        // 需要登录才能访问
        if(isLogin()) {
          next();
        }else{
          next({
            path: '/login',
            query: {
              from: to.path
            }
          })
        }
      }else {
        next();
      }
      
    })
    // 将路由实例 挂载到vm上
    const vm = new Vue({
      el: '#app',
      router
    })
  </script>
</body>

</html>